package com.cjy.crawler.service;

import java.io.IOException;

import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.mail.MailUtil;
import cn.hutool.http.HttpUtil;
import com.cjy.crawler.constants.KeywordsTypeEnum;
import org.json.JSONObject;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class ProductService {
	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	@Value("${search.keywords.url}")
	String keywordsUrl;
	@Value("${search.keywords.keys}")
	String keywords;
	@Value("${sc.ftqq.sckey}")
	String sckey;
	@Value("${sc.ftqq.url}")
	String scurl;
	@Value("${search.to}")
	private String to;

	public String getKeywords(String keywordsType) {
		// https://github.com/jy1989/SmzdmCrawler/issues/2
		if(KeywordsTypeEnum.URL.getType().equalsIgnoreCase(keywordsType)) {
			try {
				Document doc = Jsoup.connect(keywordsUrl).get();
				if(keywordsUrl.indexOf("github") >= 0) {
					return doc.selectFirst("task-lists table tr td p").text();
				}else if (keywordsUrl.indexOf("gitee") >= 0) {
					return doc.selectFirst(".git-issue-body .git-issue-description p").text();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}else if(KeywordsTypeEnum.KEYS.getType().equalsIgnoreCase(keywordsType)) {
			return keywords;
		}
		return null;
	}

	public JSONObject setItem(String article_id, String title, String price, String desc, String href, String link, String zhi, String buzhi) {
		JSONObject jo = new JSONObject();
		jo.put("article_id", article_id);
		jo.put("title", title);
		jo.put("price", price);
		jo.put("desc", desc);
		jo.put("href", href);
		jo.put("link", link);
		jo.put("zhi", zhi);
		jo.put("buzhi", buzhi);
		return jo;
	}

	public void sendMsg(JSONObject jo) throws IOException {
		StringBuffer content = new StringBuffer();
//		content.append("<div>" + jo.getString("desc") + "</div><div><b>" + jo.getString("price") + "</b></div>");
		content.append("<div><b>" + jo.getString("price") + "</b></div>");
		content.append("<div><a href='" + jo.getString("link") + "'>购买</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href='" + jo.getString("href") + "'>查看</a></div><br/>");
//		content.append("值："+ jo.getString("zhi") + "，不值："+ jo.getString("buzhi"));
		content.append("<br/><br/><br/><br/><br/>");
		content.append(jo.toString());
		String title = "价格：（"+jo.getString("price") + "）" + jo.getString("title") + "("+  jo.getString("article_id") +")";
		if(null != to && to.trim().length() > 0) {
			// 调用hutool发送邮件
			MailUtil.send(to.trim(), title, content.toString(), true, null);
		}
		if(null != sckey && sckey.trim().length() > 0) {
			String url = scurl;
			url = StrUtil.replace(url, "{sckey}", sckey);
			url = StrUtil.replace(url, "{text}", title);
			url = StrUtil.replace(url, "{desp}", content.toString());
			// 调用hutool发送Server酱请求
			HttpUtil.get(url);
		}
		logger.info("发送了通知！标题：{}，内容：{}", title, content.toString());
	}
}
